//
//  MyLuckyDrawController.m
//  trj
//
//  Created by 小小算盘 on 15/10/30.
//  Copyright © 2015年 trj-xsp. All rights reserved.
//

#import "MyLuckyDrawController.h"


#import "TestViewController.h"




#define Kscreen [UIScreen mainScreen].bounds.size
@interface MyLuckyDrawController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
{
    NSMutableArray *dataArr;
    UITableView *_tableView;
    UIView *FootView;
    //投资抽奖次数
    NSString *tender_num;
    //邀请好友抽奖次数
    NSString *trade_num;

}
@end

@implementation MyLuckyDrawController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self createTableView];
    [self sendReques];
   // NSLog(@"tender_num=%@",tender_num);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"邀请有奖";
    [self initData];
    
    
}
#pragma mark 导航栏
#pragma mark 初始化
-(void)initData
{
    dataArr = [[NSMutableArray alloc]initWithObjects:@"邀请好友抽奖:",@"投资抽奖:", nil];
    tender_num = @"0";
    trade_num = @"0";

}
#pragma mark 创建TableView
-(void)createTableView
{
    self.automaticallyAdjustsScrollViewInsets=NO;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH -64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor colorWithRed:241/255.0 green:240/255.0 blue:246/255.0 alpha:1.0];
    [self.view addSubview:_tableView];
   // [self cereateHeaderView];
    [self createFootView];
  
    

}
-(void)createFootView
{
    FootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 450)];
   // FootView.backgroundColor = [UIColor colorWithRed:241/255.0 green:240/255.0 blue:246/255.0 alpha:1.0];
    
    UILabel *TitleLable1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, KScreenW - 2*10, 16)];
    TitleLable1.text = @"邀请好友抽奖基本规则";
    TitleLable1.textColor = [UIColor colorWithRGBHex:0x2c2c2c];
    TitleLable1.font = [UIFont systemFontOfSize:15];
    [FootView addSubview:TitleLable1];
    
    UILabel *FirstLable1 = [UILabel createLabelWithFrame:CGRectMake(10, TitleLable1.frame.origin.y + TitleLable1.frame.size.height+5, KScreenW - 2*10, 15) andTitle:@"1.邀请好友，且您的好友单笔投资2000元及以上，可以获得一次抽奖机会，奖品为1%－10％加息卡，100％中奖率" withColor:[UIColor colorWithRGBHex:0x5d5d5d]];
    [FootView addSubview:FirstLable1];
    
    UILabel *SecondLable1 = [UILabel createLabelWithFrame:CGRectMake(10, FirstLable1.frame.origin.y + FirstLable1.frame.size.height+5, KScreenW - 2*10, 15) andTitle:@"2.邀请好友越多，抽奖机会也越多。上不封顶。" withColor:[UIColor colorWithRGBHex:0x5d5d5d]];
    [FootView addSubview:SecondLable1];
    
    UILabel *ThirdLable1 = [UILabel createLabelWithFrame:CGRectMake(10, SecondLable1.frame.origin.y + SecondLable1.frame.size.height+5, KScreenW - 2*10, 15) andTitle:@"3.仅限已投资的老用户参与。" withColor:[UIColor colorWithRGBHex:0x5d5d5d]];
    [FootView addSubview:ThirdLable1];
    
    UILabel *ThirdLable2 = [UILabel createLabelWithFrame:CGRectMake(10, ThirdLable1.frame.origin.y + ThirdLable1.frame.size.height, KScreenW - 2*10, 15) andTitle:@"4.加息卡利率可在“我的卡券”中查看，加息卡有效期为1个月。" withColor:[UIColor colorWithRGBHex:0x5d5d5d]];
    [FootView addSubview:ThirdLable2];
    
    UILabel *ShengMingLable = [UILabel createLabelWithFrame:CGRectMake(10, ThirdLable2.frame.origin.y +ThirdLable2.frame.size.height +5, KScreenW - 2*10, 16) andTitle:@"该抽奖与苹果公司无关." withColor:[UIColor colorWithRGBHex:0x2c2c2c]];
    [FootView addSubview:ShengMingLable];
    _tableView.tableFooterView = FootView;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 100)];
    headerView.backgroundColor=[UIColor whiteColor];
    if (section==0)
    {
        UIImageView *headerImg=[[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 60, 60)];
        headerImg.clipsToBounds=YES;
        headerImg.layer.cornerRadius=30;
        headerImg.image=[UIImage imageNamed:@"u6"];
        headerImg.alpha=0.7;
        [headerView addSubview:headerImg];
        
        //名字（改）
        UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(headerImg.frame.origin.x+headerImg.frame.size.width+10, headerImg.frame.origin.y+5, 100, 30)];
        if ([self.Mystatus integerValue] ==1) {
            nameLabel.text=self.MyName;
        }
        else{
            nameLabel.text=@"欢迎您";
        }
        nameLabel.font=[UIFont boldSystemFontOfSize:19];
        nameLabel.textColor=[UIColor grayColor];
        [headerView addSubview:nameLabel];
        
        UILabel *accountName=[[UILabel alloc]initWithFrame:CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y+nameLabel.frame.size.height, 50, 30)];
        accountName.text=@"账户:";
        accountName.font=[UIFont boldSystemFontOfSize:15];
        accountName.textColor=[UIColor grayColor];
        [headerView addSubview:accountName];
        
        //账号（改）
        UILabel *accountLabel=[[UILabel alloc]initWithFrame:CGRectMake(nameLabel.frame.origin.x+45, accountName.frame.origin.y, 150, 30)];
        accountLabel.text=self.MyNumber;
        accountLabel.font=[UIFont boldSystemFontOfSize:15];
        accountLabel.textColor=[UIColor grayColor];
        [headerView addSubview:accountLabel];
        return headerView;
    }
    
    return nil;
}
#pragma mark 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
static NSString *cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        if (indexPath.row == 0) {
            UILabel *lab= [[UILabel alloc]init];
            lab.tag = 121;
            lab.text = [NSString stringWithFormat:@"%@",trade_num];
            CGFloat W = [lab.text boundingRectWithSize:CGSizeMake(Kscreen.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size.width+5;
            lab.frame = CGRectMake(130, 7, W, 30);
            lab.textColor = [UIColor redColor];
            [cell addSubview:lab];
            UILabel *unitLabel = [[UILabel alloc]init];
            unitLabel.text = @"次";
            CGFloat W1 = [unitLabel.text boundingRectWithSize:CGSizeMake(Kscreen.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size.width+5;
            unitLabel.frame = CGRectMake(130+W, 7, W1, 30);
            unitLabel.textColor = [UIColor blackColor];
            [cell addSubview:unitLabel];
            
            
            cell.textLabel.text = [NSString stringWithFormat:@"%@",dataArr[indexPath.row]];
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenW - 70, 7, 60, 30)];
            [btn setTitle:@"去抽奖" forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:15.0];
            [btn setBackgroundImage:[UIImage imageNamed:@"内页按钮"] forState:UIControlStateNormal];
            
            btn.layer.cornerRadius = 3.0;
            btn.layer.masksToBounds = YES;
            btn.tag = 1000;
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:btn];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }
    return cell;
}
-(void)btnClick:(UIButton *)sender
{
    switch (sender.tag) {
        case 999:
        {
            TestViewController *test = [[TestViewController alloc]init];
            test.type = [NSString stringWithFormat:@"%d",(int)sender.tag-999];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:test animated:YES];
        }
            break;
            case 1000:
        {
            TestViewController *test = [[TestViewController alloc]init];
            test.type = [NSString stringWithFormat:@"%d",(int)sender.tag-999];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:test animated:YES];
        }
            break;
        default:
            break;
    }
   

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 100;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *HeaderView = [[UIView alloc]init];
    HeaderView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
    return HeaderView;

}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;

}
#pragma mark 网络请求

-(void)sendReques{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_DRAWNUM];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        tender_num = requestDic[@"tender_num"];
        trade_num = requestDic[@"trade_num"];
        UILabel *label = [self.view viewWithTag:121];
        label.text = trade_num;
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}


@end
